<?php
namespace Weibo\Controller;
use Common\Controller\AdminbaseController;

/**
 * 热度管理
 * @author Hongwei 2017-2-20
 */
class AdminMacroController extends AdminbaseController {

	protected $weibo_model;
	protected $weibo_hot_model;
	protected $weibo_search_stat_model;
	protected $weibo_search_hot_model;

	public function _initialize() {
		parent::_initialize();
		$this ->weibo_model = D('weibo');
		$this ->weibo_hot_model = D('weibo_hot');
		$this ->weibo_search_stat_model = D('weibo_search_stat');
		$this ->weibo_search_hot_model = D('weibo_search_hot');
	}

	/**
	 * 热门微博列表 100条
	 */
	public function hotWeibo() {

		$db_prefix=C('DB_PREFIX');

		//列表
		$weibo_hot_list = $this->weibo_hot_model
			-> alias('a')
			-> field("a.*,b.content")
			-> join($db_prefix.'weibo b ON a.weibo_id = b.weibo_id')
			-> limit(0,100)
			-> order(array('orderby'=>'asc'))
			-> select();

		$this -> assign('weibo_host_list', $weibo_hot_list);
		$this -> display ();

		//TODO 批量排序
	}
	
	/**
	 * 刷新热门微博 100条
	 * TODO  以后再做，没找到方法：这里直接 insert into select 就可以，然后$ordery 用数据表的形式查询展示
	 */
	public function refreshHotWeibo() {

		$db_prefix=C('DB_PREFIX');

		//第一步：删除操作 注：不能用truncate，因为它会释放表，导致错误发生，所以只能循环删除
		$result_hot = $this->weibo_hot_model->select();

		foreach ($result_hot as $key => $value){
			$hot_id = $result_hot[$key]['hot_id'];
			$this->weibo_hot_model->where($hot_id)->delete();
		}

		//第二步：读数据，表：pk_weibo( 注：查24小时或一星期之内，目前查一周数据，24小时内数据量太少)
		//全部数据
		//$sql_weibo = 'select *, (forwarded_count+ commented_count+ praised_count) as score from '.$db_prefix.'weibo order by score desc limit 0,100';
		//1周内的sql
		$sql_weibo = 'select *, (forwarded_count+ commented_count+ praised_count) as score from '.$db_prefix.'weibo where hour(TIMEDIFF(NOW(),FROM_UNIXTIME(`create_time`, \'%Y-%m-%d %H:%i:%S\'))) <24*7 order by score,create_time desc limit 0,100'; //一周的数据
		$result = $this->weibo_model->query($sql_weibo);
		foreach ($result as $key => $value){
			$data['weibo_id'] = $value['weibo_id'];
			$data['orderby'] = $key+1;
			$this->weibo_hot_model->add($data);
		}

		//第三步：列表
		$result_hot2 = $this->weibo_hot_model
			-> alias('a')
			-> field("a.*,b.content")
			-> join($db_prefix.'weibo b ON a.weibo_id = b.weibo_id')
			-> order(array('orderby'=>'asc'))
			-> select();

		$this->redirect(U('AdminMacro/hotweibo'));


	}
	
	/**
	 * 热门微博批量操作
	 */
	public function hotWeiboBat($type='order') {

		//排序
		if ($type == 'order') {
			$order_array = I('post.orderby');
			if ($this->weibo_hot_model -> saveBatch($order_array, 'hot_id','orderby') !== FALSE) {
				$this -> success('排序成功');
			} else {
				$this -> error('排序出错');
			}
		//删除
		} elseif ($type == 'delete') {
			$ids = I('post.ids');
			if ($this->weibo_hot_model -> delete(implode(',',$ids)) !== FALSE) {
				$this -> success('删除成功');
			} else {
				$this -> error('删除失败');
			}
		}
	}
	
	/**
	 * 热门搜索列表 100条
	 */
	public function hotSearch() {

		$db_prefix=C('DB_PREFIX');
		$sql = "select * from ".$db_prefix."weibo_search_hot order by hot_id desc limit 0,100";

		//热门微博列表（所有的倒序排列）
		$weibo_search_hot = $this->weibo_search_hot_model->query($sql);

		$this -> assign('list', $weibo_search_hot);
		$this -> display ();

	}
	
	/**
	 * 刷新热门搜索
	 * 根据现有数据计算出来
	 */
	public function refreshHotSearch() {

		$db_prefix=C('DB_PREFIX');

		//删除操作 注：不能用truncate，因为它会释放表，导致错误发生，所以只能循环删除
		$result_search_hot = $this->weibo_search_hot_model->select();

		foreach ($result_search_hot as $key => $value){
			$hot_id = $result_search_hot[$key]['hot_id'];
			$this->weibo_search_hot_model->where($hot_id)->delete();
		}

		//读数据，表：pk_weibo_stat( TODO 查24小时或一星期之内，但目前数据少)
		$sql_weibo_stat = "select keyword,count(keyword) as count from ".$db_prefix."weibo_search_stat where keyword<>'' group by keyword order by count desc limit 0,100"; //经检测sql语句没错
		$result = $this->weibo_search_hot_model->query($sql_weibo_stat);
		foreach ($result as $key => $value){
			$data['keyword'] = $value['keyword'];
			$data['orderby'] = $key+1;
			//$data['expiry'] = ??;  //TODO
			$this->weibo_search_hot_model->add($data);
		}

		$this->redirect(U('AdminMacro/hotSearch'));
	}
	
	/**
	 * 热门搜索批量操作
	 */
	public function hotSearchBat($type='order') {

		//排序
		if ($type == 'order') {
			$order_array = I('post.orderby');
			if ($this->weibo_search_hot_model -> saveBatch($order_array, 'hot_id','orderby') !== FALSE) {
				$this -> success('排序成功');
			} else {
				$this -> error('排序出错');
			}
		//删除
		} elseif ($type == 'delete') {
			$ids = I('post.ids');
			if ($this->weibo_search_hot_model -> delete(implode(',',$ids)) !== FALSE) {
				$this -> success('删除成功');
			} else {
				$this -> error('删除失败');
			}
		}
	}
}